package com.stuManager.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stuManager.model.CommonResponseModel;
import com.stuManager.service.DBUserService;
import com.stuManager.service.StudentService;
import com.stuManager.util.CookiesUtil;
import com.stuManager.util.ReadPostBodyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
    DBUserListServlet
    学生管理系统管理员列表接口
    返回当前的管理员信息，管理员暂时不能通过接口添加，直接在数据库加管理员
 */
@WebServlet(name = "DBUserListServlet", value = "/dbuserlist")
public class DBUserListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        CommonResponseModel m = new CommonResponseModel();
        //先获取cookie信息鉴权
        Cookie token = CookiesUtil.getCookieByName(request, "token");
        if (token == null) {
            m.setErrorCode(1);
            m.setMessage("用户未授权");
        }
        else {
            //获取一个service来处理业务
            DBUserService service = new DBUserService();
            m = service.fetchAllUser(token.getValue());
        }
        String jsonString = JSON.toJSONString(m);
        System.out.println("返回数据\n" + jsonString);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print(jsonString);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
